<?php

class desktop_ctl_passport extends desktop_controller {

    var $login_times_error = 3;

    var $certcheck = false;

    public function __construct($app) {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    function index() {
        
        // 在登录页面时，验证码之后,可实现的servicelist
        $present = kernel::servicelist("passport_index_present");
        foreach ($present as $item) {
            if (is_object($item) && method_exists($item, "handle"))
                $item->handle($this);
        }
        
        $auth = pam_auth::instance(pam_account::get_account_type($this->app->app_id));
        $auth->set_appid($this->app->app_id);
        $auth->set_redirect_url($_GET['url']);
        $this->pagedata['desktop_url'] = kernel::router()->app->base_url(1);
        
        // 判断登录的标志是哪个系统的
        /*
         * $commerce_class = kernel::single('system_commerce');
         * if (! $commerce_class->get_commerce_version()) {
         * $this->pagedata['cross_call_url'] = base64_encode(kernel::router()->app->base_url(1) . 'index.php?ctl=passport&act=cross_call');
         * } else {
         * $this->pagedata['Commerce'] = 'yes';
         * $this->pagedata['img_url'] = app::get('desktop')->res_url . '/login.png';
         * }
         */
        $conf = base_setup_config::deploy_info();
        
        foreach (kernel::servicelist('back_passport') as $k => $passport) {
            if ($auth->is_module_valid($k, $this->app->app_id)) {
                $this->pagedata['passports'][] = array(
                    'name' => $auth->get_name($k) ? $auth->get_name($k) : $passport->get_name(),
                    'html' => $passport->get_login_form($auth, 'desktop', 'basic-login.html', $pagedata)
                );
            }
        }
        $xml = file_get_contents(ROOT_DIR . '/config/deploy.xml');
        $deploy = kernel::single('base_xml')->xml2array($xml, 'base_deploy');
        $local_has_update = false;
        $this->pagedata['ec_deploy'] = $deploy;
        
        $this->pagedata['product_key'] = $conf['product_key'];
        $this->display('login.html');
    }

    function gen_vcode() {
        $vcode = kernel::single('base_vcode', 'admin');
        $vcode->length(4);
        $vcode->verify_key($this->app->app_id);
        $vcode->display();
    }

    function cross_call() {
        return true;
    }

    function logout($backurl = 'index.php') {
        $this->begin('javascript:Cookie.dispose("basicloginform_password");Cookie.dispose("basicloginform_autologin"); location="' . kernel::router()->app->base_url(1) . '"');
        $this->user->login();
        $this->user->logout();
        $auth = pam_auth::instance(pam_account::get_account_type($this->app->app_id));
        foreach (kernel::servicelist('back_passport') as $k => $passport) {
            if ($auth->is_module_valid($k, $this->app->app_id))
                $passport->loginout($auth, $backurl);
        }
        $sessionLib = kernel::single('base_session');
        $sessionLib->is_back_sess_key();
        $sessionLib->destory();
        $this->end('true', app::get('desktop')->_('已成功退出系统,正在转向...'));
    }
}
